package com.mtz.service.impl;

import com.mtz.dateobject.Question;
import com.mtz.enums.DeleteEnum;
import com.mtz.enums.ResultEnum;
import com.mtz.exception.MtzException;
import com.mtz.repository.QuestionRepository;
import com.mtz.service.QuestionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 煜赜
 * @date 2018/11/27 0027 上午 9:33
 */
@Service
@Slf4j
public class QuestionServiceImpl implements QuestionService {

    @Autowired
    private QuestionRepository questionRepository;

    @Override
    public Question findQuesById(Integer id) {
        // 检查Id是否存在
        if (!questionRepository.findById(id).isPresent()) {
            throw new MtzException(ResultEnum.ID_NOT_EXIST);
        }
        Question question = questionRepository.findById(id).get();

        return question;
    }

    @Override
    public List<Question> findQuesByKnowledgeType(Integer knowledgeType) {
        return questionRepository.findByKnowledgeType(knowledgeType);
    }

    @Override
    public List<Question> findQuesByKeyword(String keyword) {
        List<Question> questionList = questionRepository.findByKeyword(keyword);
        if (questionList.size() == 0) {
            throw new MtzException(ResultEnum.KEY_NOT_EXIST);
        }
        return questionList;
    }

    @Override
    public List<Question> findQuesByIsDeleted(Integer isDeleted) {
        List<Question> questionList = questionRepository.findByIsDeleted(isDeleted);
        if (questionList.size() == 0) {
            return null;
        }
        return questionList;
    }

    @Override
    public List<Question> findAll() {
        return questionRepository.findAll();
    }

    @Override
    public Question addQuestion(Integer knowledgeType, String question, String keyword, Integer isDeleted) {
        Question newQuestion = new Question();
        newQuestion.setKnowledgeType(knowledgeType);
        newQuestion.setQuestion(question);
        newQuestion.setKeyword(keyword);
        if (isDeleted != null) {
            newQuestion.setIsDeleted(isDeleted);
        }

        return questionRepository.save(newQuestion);
    }

    @Override
    public void setQuesAnswerId(Integer questionId, Integer answerId) {
        // 检查Id是否存在
        if (!questionRepository.findById(questionId).isPresent()) {
            throw new MtzException(ResultEnum.ID_NOT_EXIST);
        }
        Question question = questionRepository.findById(questionId).get();
        question.setAnswerId(answerId);
        questionRepository.save(question);
    }

    @Override
    public void changeKnowledgeType(Integer questionId, Integer knowledgeType) {
        // 检查Id是否存在
        if (!questionRepository.findById(questionId).isPresent()) {
            throw new MtzException(ResultEnum.ID_NOT_EXIST);
        }
        Question question = questionRepository.findById(questionId).get();
        question.setKnowledgeType(knowledgeType);
        questionRepository.save(question);
    }

    @Override
    public Question editQuestion(Integer questionId, String question, String keyword) {
        if (!questionRepository.findById(questionId).isPresent()) {
            throw new MtzException(ResultEnum.ID_NOT_EXIST);
        }
        Question oldQuestion = questionRepository.findById(questionId).get();
        oldQuestion.setQuestion(question);
        oldQuestion.setKeyword(keyword);
        return questionRepository.save(oldQuestion);
    }

    @Override
    public void publicQuesById(Integer id) {
        // 检查Id是否存在
        if (!questionRepository.findById(id).isPresent()) {
            throw new MtzException(ResultEnum.ID_NOT_EXIST);
        }
        Question question = questionRepository.findById(id).get();
        question.setIsDeleted(DeleteEnum.PUBLISH.getCode());
        questionRepository.save(question);
    }

    @Override
    public void invalidQuesById(Integer id) {
        // 检查Id是否存在
        if (!questionRepository.findById(id).isPresent()) {
            throw new MtzException(ResultEnum.ID_NOT_EXIST);
        }
        Question question = questionRepository.findById(id).get();
        question.setIsDeleted(DeleteEnum.INVALID.getCode());
        questionRepository.save(question);
    }

    @Override
    public void deleteQuesById(Integer id) {
        // 检查Id是否存在
        if (!questionRepository.findById(id).isPresent()) {
            throw new MtzException(ResultEnum.ID_NOT_EXIST);
        }
        Question question = questionRepository.findById(id).get();
        question.setIsDeleted(DeleteEnum.DELETED.getCode());
        questionRepository.save(question);
    }

    @Override
    public void trueDelete(Integer id) {
        // 检查Id是否存在
        if (!questionRepository.findById(id).isPresent()) {
            throw new MtzException(ResultEnum.ID_NOT_EXIST);
        }
        questionRepository.deleteById(id);
    }

    @Override
    public ArrayList findKeywords() {
        ArrayList<String> keywords = new ArrayList<String>();
        List<Question> questionList = questionRepository.findAll();
        for (Question question : questionList) {
            keywords.add(question.getKeyword());
        }
        return keywords;
    }
}
